package cn.huiyunche.admin.service.impl;

import cn.huiyunche.admin.service.DOilPriceService;
import cn.huiyunche.base.service.enums.OilProductsEnum;
import cn.huiyunche.base.service.mappers.DOilPriceMapper;
import cn.huiyunche.base.service.model.DOilPrice;
import cn.huiyunche.base.service.model.DOilPriceExample;
import cn.huiyunche.base.service.vo.DOilPriceVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by jxy on 17/1/5.
 */
@Service
public class DOilPriceServiceImpl implements DOilPriceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DOilPriceServiceImpl.class);

    @Autowired
    private DOilPriceMapper dOilPriceMapper;

    public Map<String, Object> getListByPage(PageVo page,DOilPriceVo dOilPriceVo){
        LOGGER.info("getListByPage params page: {}",page);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> paramsMap = new HashMap<>();
        String orderByClause = StringUtils.isNotBlank(page.getOrder()) == true ? page.getOrder() : " id DESC";
        page.setTotalRecord(dOilPriceMapper.countByCondations(paramsMap));
        DOilPriceExample dOilPriceExample = new DOilPriceExample();
        dOilPriceExample.setLimitStart(page.getStartIndex());
        dOilPriceExample.setLimitEnd( page.getPageSize());
        dOilPriceExample.setOrderByClause(orderByClause);
        //查询条件判空
        if (dOilPriceVo.getOilProducts() != null ){
            dOilPriceExample.createCriteria().andOilProductsEqualTo(dOilPriceVo.getOilProducts());
        }
        if (dOilPriceVo.getUseBeginTime() != null && dOilPriceVo.getUseEndTime() != null ){
            dOilPriceExample.createCriteria().andStartUseTimeBetween(dOilPriceVo.getUseBeginTime(),dOilPriceVo.getUseEndTime());
        }else{
            if (dOilPriceVo.getUseBeginTime() != null ){
                dOilPriceExample.createCriteria().andStartUseTimeGreaterThan(dOilPriceVo.getUseBeginTime());
            }
            if (dOilPriceVo.getUseEndTime() != null ){
                dOilPriceExample.createCriteria().andStartUseTimeLessThan(dOilPriceVo.getUseEndTime());
            }
        }

        List<DOilPrice> dOilPriceList = dOilPriceMapper.selectByExample(dOilPriceExample);
        List<JSONObject> res = new ArrayList<>();
        for (DOilPrice dOilPrice : dOilPriceList){
            JSONObject json = (JSONObject) JSON.toJSON(dOilPrice);
            json.put("oilProductsText", OilProductsEnum.getByValue(json.getIntValue("oilProducts"))==null?"":OilProductsEnum.getByValue(json.getIntValue("oilProducts")).getText());
            res.add(json);
        }
        map.put("page", page);
        map.put("dOilPriceList", res);
        return map;
    }

    public void addDOilPrice(DOilPrice dOilPrice){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        LOGGER.info("addDOilPrice params dOilPrice: {}",dOilPrice);
        //新添加的起效时间 必须大于已经存在的数据的起效时间
        DOilPriceExample dOilPriceExample = new DOilPriceExample();
        String orderByClause = " start_use_time DESC";
        dOilPriceExample.setOrderByClause(orderByClause);
        dOilPriceExample.createCriteria().andOilProductsEqualTo(dOilPrice.getOilProducts()).andOilTypeEqualTo(dOilPrice.getOilType());
        List<DOilPrice> dOilPriceList = dOilPriceMapper.selectByExample(dOilPriceExample);

        if (dOilPriceList.size() > 0 && dOilPriceList.get(0).getStartUseTime().getTime() >= dOilPrice.getStartUseTime().getTime()){
            throw new BusinessException("起效时间必须大于"+df.format(dOilPriceList.get(0).getStartUseTime()));
        }
        if (dOilPrice.getSeal() == null){
            dOilPrice.setSeal(new BigDecimal(1));
        }
        dOilPrice.setIsEnable(true);
        dOilPrice.setCreateTime(new Date());
        dOilPrice.setUpdateTime(new Date());
        int res = dOilPriceMapper.insertSelective(dOilPrice);
        if (res != 1){
            throw new BusinessException("数据插入油价表失败");
        }

    }

    public void isEnable(Long id,Boolean isEnable){
        LOGGER.info("isEnable params id: {};isEnable:{}",id,isEnable);
        DOilPrice dOilPrice = new DOilPrice();
        dOilPrice.setId(id);
        dOilPrice.setIsEnable(isEnable);
        int res = dOilPriceMapper.updateByPrimaryKeySelective(dOilPrice);
        if (res != 1){
            throw new BusinessException("设置可用不可用数据失败");
        }

    }

}
